package com.learn.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.learn.common.utils.StringUtils;
import com.learn.core.domain.*;
import com.learn.core.mapper.LCompetitionMapper;
import com.learn.core.mapper.LCompetitionPaperMapper;
import com.learn.core.mapper.LCompetitionQuestionMapper;
import com.learn.core.mapper.LQuestionsMapper;
import com.learn.core.service.ILCompetitionPaperService;
import com.learn.framework.service.impl.BaseServiceImpl;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Description
 *
 * @author zhaohongyu
 * @ClassName LCompetitionPaperServiceImpl
 * @date 2021.05.28 10:40
 */
@Service
public class LCompetitionPaperServiceImpl extends BaseServiceImpl<LCompetitionPaperMapper, LCompetitionPaper> implements ILCompetitionPaperService {

    @Autowired
    LCompetitionMapper lCompetitionMapper;
    @Autowired
    LQuestionsMapper lQuestionsMapper;
    @Autowired
    LCompetitionQuestionMapper lCompetitionQuestionMapper;
    @Autowired
    LCompetitionPaperMapper lCompetitionPaperMapper;

    @Override
    public void competitionPaper(Integer id,String userId) throws Exception {
        LCompetition competition = lCompetitionMapper.selectById(id);
        // 生成制定条数时间
        for (int i = 1; i <= competition.getRandomSeveral();i++) {
//            //TODO 根据考次规定生成试卷
            QueryWrapper<LCompetitionQuestion> questionQueryWrapper = new QueryWrapper<LCompetitionQuestion>();
            questionQueryWrapper.eq("competition_id", id);
            List<LCompetitionQuestion> list = lCompetitionQuestionMapper.selectList(questionQueryWrapper);
            Map<String, Object> map = new HashMap<>();
            for (LCompetitionQuestion lCompetitionQuestion : list) {
                if (1 == lCompetitionQuestion.getCompetitionQuestionId()) {
                    map.put("radio", examQuestionsList(lCompetitionQuestion,lCompetitionQuestion.getNumber(),lCompetitionQuestion.getCompetitionTypeId()));
                } else if (2 == lCompetitionQuestion.getCompetitionQuestionId()) {
                    map.put("check", examQuestionsList(lCompetitionQuestion,lCompetitionQuestion.getNumber(),lCompetitionQuestion.getCompetitionTypeId()));
                } else if (3 == lCompetitionQuestion.getCompetitionQuestionId()) {
                    map.put("judge", examQuestionsList(lCompetitionQuestion,lCompetitionQuestion.getNumber(),lCompetitionQuestion.getCompetitionTypeId()));
                }
            }
            JSONObject jsonObject = JSONObject.fromObject(map);

            // 生产试卷
            LCompetitionPaper lCompetitionPaper = new LCompetitionPaper();
            lCompetitionPaper.setCompetitionId(id);
            lCompetitionPaper.setCompetitionQuestionsId(jsonObject.toString());
            lCompetitionPaper.setCreateTime(new Date());
            lCompetitionPaper.setCreateUser(userId);
            lCompetitionPaperMapper.insert(lCompetitionPaper);
        }
    }

    private List<Integer> examQuestionsList(LCompetitionQuestion lCompetitionQuestion, Integer number, Integer questionsTypeId){
        List<Integer> stringList = new ArrayList<>();
        QueryWrapper<LQuestions> lQuestionsQueryWrapper = new QueryWrapper<LQuestions>();
        lQuestionsQueryWrapper.eq("questions_type",questionsTypeId);
        if(StringUtils.isNotEmpty(lCompetitionQuestion.getKnowledge()))
            lQuestionsQueryWrapper.like("knowledge",lCompetitionQuestion.getKnowledge());
        if(StringUtils.isNotEmpty(lCompetitionQuestion.getDifficulty()))
            lQuestionsQueryWrapper.eq("questions_difficulty",lCompetitionQuestion.getDifficulty());
        lQuestionsQueryWrapper.orderByAsc("rand()");
        lQuestionsQueryWrapper.last(" limit " + number);
        List<LQuestions> lQuestionsList = lQuestionsMapper.selectList(lQuestionsQueryWrapper);
        for (LQuestions lQuestions : lQuestionsList) {
            stringList.add(lQuestions.getQuestionsId());
        }
        return stringList;
    }
}
